package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.common.FollowType;
import com.example.kehoobackend.domain.Follow;
import com.example.kehoobackend.domain.Topic;
import com.example.kehoobackend.persistance.TopicMapper;
import com.example.kehoobackend.service.FollowService;
import com.example.kehoobackend.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("topicService")
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    FollowService followService;

    @Override
    public CommonResponse<List<Topic>> getAllTopTopic() {
        List<Topic> topics = list();
        List<Topic> root_topic = new ArrayList<>();
        for(Topic topic : topics){
            if(topic.getParent_topic_id() == null)
                root_topic.add(topic);
        }
        return CommonResponse.createForSuccess("查询所有一级话题",root_topic);
    }

    @Override
    public CommonResponse<Topic> getParentTopicByTopicId(Integer topicId) {
        Topic parentTopic = getById(topicId);
        return CommonResponse.createForSuccess("查询父话题",parentTopic);
    }

    @Override
    public CommonResponse<List<Topic>> getChildTopicByTopicId(Integer topicId) {
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_topic_id",topicId);
        List<Topic> topics = list(queryWrapper);
        return CommonResponse.createForSuccess("查询子话题",topics);
    }

    @Override
    public Topic addTopic(Topic topic) {
        //TODO:图片保存
        save(topic);
        return topic;
    }

    @Override
    public boolean updateTopicImage(Integer topic_id, String topic_image) {
        //TODO:更新图片
        Topic topic = getById(topic_id);
        topic.setTopic_image(topic_image);
        return updateById(topic);
    }

    @Override
    public boolean updateTopicDescription(Integer topic_id, String topic_description) {
        Topic topic = getById(topic_id);
        topic.setTopic_desc(topic_description);
        return updateById(topic);
    }

    @Override
    public boolean updateTopicName(Integer topic_id, String topic_name) {
        Topic topic = getById(topic_id);
        topic.setTopic_name(topic_name);
        return updateById(topic);
    }

    @Override
    public CommonResponse<List<Topic>> getAllTopicList() {
        return CommonResponse.createForSuccess("返回所有话题",list());
    }

    @Override
    public boolean deleteTopic(Integer topic_id) {
        removeById(topic_id);

        //删除对话题所有关注
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.eq("to_id",topic_id).eq("type", FollowType.TOPIC);
        List<Follow> followList = followService.list(followQueryWrapper);
        for(Follow follow : followList){
            followService.removeById(follow);
        }


        return true;
    }

    @Override
    public CommonResponse<List<Map<String,Object>>> getQuestionCountsByTopicName() {
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("topic_name","COUNT(*) AS count")
                .groupBy("topic_name").orderByDesc("count");

        //result中每条记录为一个Map，Map的键为列名，值为相应的字段值
        List<Map<String, Object>> result = listMaps(queryWrapper);
        List<Map<String, Object>> dividedResult = new ArrayList<>();
        //从第5个元素开始全部记为其他
        Map<String,Object> otherMap = new HashMap<>();
        Integer otherCounts = 0;

        for(int i=0;i<result.size();i++){
            if(i>5){
                otherCounts+=Integer.parseInt(String.valueOf(result.get(i).get("count")));
            }else{
                dividedResult.add(result.get(i));
            }
        }
        otherMap.put("topic_name","其他");
        otherMap.put("count",otherCounts);
        dividedResult.add(otherMap);

        return CommonResponse.createForSuccess("查询话题名以及该话题名包含的问题数量",dividedResult);
    }
}




